package com.example.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Carousel;
import com.example.mapper.CarouselMapper;
import com.example.service.CarouselService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class CarouselServiceImpl extends ServiceImpl<CarouselMapper, Carousel> implements CarouselService {

    @Resource
    private CarouselMapper carouselMapper;

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public List<Carousel> findByLimit(int num) {

        return carouselMapper.selectByLimit(num);
    }

    @Override
    public IPage<Carousel> findCarouselsByPage(String title, int page, int size) {
        Page<Carousel> pageInfo = new Page<>(page,size);
        QueryWrapper<Carousel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(title),"img_title",title);
        queryWrapper.orderByDesc("create_date");
        //根据分页插件对象里的分页条件查询数据
        IPage<Carousel> iPage = carouselMapper.selectPage(pageInfo,queryWrapper);
        return iPage;
    }
}
